#include <stdio.h>
#include <curl/curl.h>

/*
 * 天网格式存放网页
 * */
struct tw_header {
	float version;
	const char *url;
	const char *origin;
	long response_code;
	time_t crawl_time;
	const char *ip;
	long unzip_length;
	long length;
};

char *tw_header2str(struct tw_header *header)
{
	return NULL;
}

int output_page(FILE *fp, CURL *curl, const char *page, const char *url, int page_size, int zip_flag)
{
	struct tw_header twh;
	twh.version = 1.0;
	curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &twh.url);
	curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &twh.response_code);
	twh.origin = url;
	twh.crawl_time = time(NULL);
	curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &twh.ip);
	if (zip_flag == 0) {
		twh.unzip_length = -1;
	}
	else 
		twh.unzip_length = -1;
	twh.length = page_size;
	fprintf(fp, "version:%.1f\n", twh.version);
	fprintf(fp, "url:%s\n", twh.url);
	fprintf(fp, "origin:%s\n", twh.origin);
	struct tm tmp;
	gmtime_r(&twh.crawl_time, &tmp);
	char out[200];
	strftime(out, sizeof out, "%Y-%m-%d %H:%M:%S", &tmp);
	fprintf(fp, "date:%s\n", out);
	fprintf(fp, "ip:%s\n", twh.ip);
	if (twh.unzip_length > 0)
		fprintf(fp, "unzip-length:%ld\n", twh.unzip_length);
	fprintf(fp, "length:%ld\n", twh.length);
	fprintf(fp, "\n");
	fwrite(page, 1, page_size, fp);
	fprintf(fp, "\n");
	return 0;
}
